//
//  AuthenMiniLegalVM.swift
//  kxf
//
//  Created by Mac on 2024/3/7.
//

import Foundation
import UIKit
import MCToast

class AuthenMiniLegalVM: UIView {
    
    var selfHeight = kFitWidth(10)
    
    var idFrontMsgDict = NSDictionary()
    var idBackMsgDict = NSDictionary()
    var bankMsgDict = NSDictionary()
    
    var nextBlock:(()->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: frame.size.height))
        self.selfHeight = frame.size.height
        self.backgroundColor = .COLOR_LIGHT_GREY
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var scrollView : UIScrollView = {
        let scro = UIScrollView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: self.selfHeight))
        scro.backgroundColor = .COLOR_LIGHT_GREY
        if #available(iOS 11.0, *) {
            scro.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        scro.bounces = true
        
        return scro
    }()
    lazy var photoTitleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.titleLabel.text = "店主有关资料"
        return vm
    }()
    lazy var photoVm : AuthenMiniLegalPhotoVM = {
        let vm = AuthenMiniLegalPhotoVM.init(frame: CGRect.init(x: 0, y: self.photoTitleVm.frame.maxY, width: 0, height: 0))
        vm.idOcrFrontBlock = {(dict)in
            self.idFrontMsgDict = dict
            self.updateIdCardFrontMsg()
        }
        vm.idOcrBackBlock = {(dict)in
            self.idBackMsgDict = dict
            self.udpateIdCardBackMsg()
        }
        vm.bankOcrFrontBlock = {(dict)in
            self.bankMsgDict = dict
            self.udpateBankCardBackMsg()
        }
        return vm
    }()
    lazy var msgVm : AuthenMiniLegalMsgVM = {
        let vm = AuthenMiniLegalMsgVM.init(frame: CGRect.init(x: 0, y: self.photoVm.frame.maxY+kFitWidth(10), width: 0, height: 0))
        
        return vm
    }()
    lazy var nextBtn : UIButton = {
        let btn = UIButton()
        btn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVm.frame.maxY+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
        btn.setTitle("下一步", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        
        btn.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
        
        return btn
    }()
}

extension AuthenMiniLegalVM{
    @objc func nextAction() {
        if self.nextBlock != nil{
            self.nextBlock!()
        }
    }
    func checkValue() -> Bool {
        getValue()
        
        if AuthenMiniMsgModel.shareInstance.photoImgUrlIdCardFront.count < 2 || AuthenMiniMsgModel.shareInstance.photoImgUrlIdCardFront == ""{
            MCToast.mc_text("请上传身份证人像面")
            return false
        }
        if AuthenMiniMsgModel.shareInstance.photoImgUrlIdCardBack.count < 2 || AuthenMiniMsgModel.shareInstance.photoImgUrlIdCardBack == ""{
            MCToast.mc_text("请上传身份证国徽面")
            return false
        }
        if AuthenMiniMsgModel.shareInstance.photoImgUrlBankCardFront.count < 2 || AuthenMiniMsgModel.shareInstance.photoImgUrlBankCardFront == "" {
            MCToast.mc_text("请上传银行卡照片")
            return false
        }
        if AuthenMiniMsgModel.shareInstance.photoImgUrlIdCardInhand.count < 2 || AuthenMiniMsgModel.shareInstance.photoImgUrlIdCardInhand == "" {
            MCToast.mc_text("请上传手持身份证照")
            return false
        }
        if AuthenMiniMsgModel.shareInstance.legalName == "" || AuthenMiniMsgModel.shareInstance.legalName.count < 2 {
            MCToast.mc_text("请输入证件姓名")
            return false
        }
        if AuthenMiniMsgModel.shareInstance.legalIdCardNo == "" || AuthenMiniMsgModel.shareInstance.legalIdCardNo.count < 10 {
            MCToast.mc_text("请输入证件号码")
            return false
        }
        if AuthenMiniMsgModel.shareInstance.legalIdCardStartTime.count < 4 || AuthenMiniMsgModel.shareInstance.legalIdCardEndTime.count < 4{
            MCToast.mc_text("请选择证件有效期")
            return false
        }
        if AuthenMiniMsgModel.shareInstance.legalAddress.count < 3{
            MCToast.mc_text("请输入身份证地址")
            return false
        }
        if AuthenMiniMsgModel.shareInstance.legalBankName.count < 2 {
            MCToast.mc_text("请输入银行名称")
            return false
        }
        if AuthenMiniMsgModel.shareInstance.legalBankBranchCode == "" || AuthenMiniMsgModel.shareInstance.legalBankBranchCode.count < 2 {
            MCToast.mc_text("请选择开户支行")
            return false
        }
        if AuthenMiniMsgModel.shareInstance.legalBankNo.count < 8 {
            MCToast.mc_text("请输入银行卡号")
            return false
        }
        if !WHBaseViewVC().judgePhoneNumber(phoneNum: AuthenMiniMsgModel.shareInstance.legalBankPhone){
            MCToast.mc_text("请输入正确的预留手机号")
            return false
        }
        return true
    }
}

extension AuthenMiniLegalVM{
    func initUI() {
        addSubview(scrollView)
        scrollView.addSubview(photoTitleVm)
        scrollView.addSubview(photoVm)
        scrollView.addSubview(msgVm)
        scrollView.addSubview(nextBtn)
        
        scrollView.contentSize = CGSize.init(width: 0, height: nextBtn.frame.maxY+kFitWidth(20))
    }
    func updateIdCardFrontMsg() {
        msgVm.idNameVm.textField.text = self.idFrontMsgDict["name"]as? String ?? ""
        msgVm.idNoVm.textField.text = self.idFrontMsgDict["code"]as? String ?? ""
        msgVm.idAddressVm.textField.text = self.idFrontMsgDict["address"]as? String ?? ""
    }
    func udpateIdCardBackMsg() {
        msgVm.idStartTimeVm.setLabelContent(content: Date().changeDateFormatter(dateString: "\(idBackMsgDict["issueDate"]as? String ?? "")"))
        msgVm.idEndTimeVm.setLabelContent(content: Date().changeDateFormatter(dateString: "\(idBackMsgDict["expiryDate"]as? String ?? "")"))
        
    }
    
    func udpateBankCardBackMsg() {
        msgVm.bankNameVm.textField.text = bankMsgDict["bank_name"]as? String ?? ""
//        msgVm.bankNoVm.textField.text = bankMsgDict["bank_card_number"]as? String ?? ""
        let bankNo = bankMsgDict["bank_card_number"]as? String ?? ""
        msgVm.bankNoVm.textField.text = bankNo.replacingOccurrences(of: " ", with: "")
    }
    func updateUIForRefuse() {
        photoVm.updateUIForRefuse()
        msgVm.updateUIForRefuse()
    }
    //设置为只读模式
    func setReadMode() {
//        photoVm.isUserInteractionEnabled = false
//        msgVm.isUserInteractionEnabled = false
//        
        photoVm.setReadMode()
        msgVm.setReadMode()
        nextBtn.isHidden = true
    }
}

extension AuthenMiniLegalVM{
    func getValue(){
        AuthenMiniMsgModel.shareInstance.legalName = msgVm.idNameVm.textField.text ?? ""
        AuthenMiniMsgModel.shareInstance.legalIdCardNo =
        msgVm.idNoVm.textField.text ?? ""
        AuthenMiniMsgModel.shareInstance.legalIdCardStartTime = msgVm.idStartTimeVm.rightLabel.text ?? ""
        AuthenMiniMsgModel.shareInstance.legalIdCardEndTime = msgVm.idEndTimeVm.rightLabel.text ?? ""
        AuthenMiniMsgModel.shareInstance.legalAddress = msgVm.idAddressVm.textField.text ?? ""
        AuthenMiniMsgModel.shareInstance.legalBankName = msgVm.bankNameVm.textField.text ?? ""
        AuthenMiniMsgModel.shareInstance.legalBankBranchName = msgVm.bankBranchVm.rightLabel.text ?? ""
        AuthenMiniMsgModel.shareInstance.legalBankNo = msgVm.bankNoVm.textField.text ?? ""
        AuthenMiniMsgModel.shareInstance.legalBankPhone = msgVm.phoneVm.textField.text ?? ""
    }
    
}
